﻿using System.Windows;
using System.Windows.Input;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Common;
using System.ComponentModel;

namespace CPETwit.WPFClient.ViewModel
{
    public class AuthentificationViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Fields

        private readonly WCFServiceCaller _serviceCaller;
        private string _userName;
        private string _password;
        private Visibility _errorPanelVisibility;
        private ICommand _loginCommand;

        #endregion

        #region Properties

        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged("Password");
            }
        }
        public Visibility ErrorPanelVisibility
        {
            get { return _errorPanelVisibility; }
            set
            {
                _errorPanelVisibility = value;
                OnPropertyChanged("ErrorPanelVisibility");
            }
        }
        public ICommand LoginCommand
        {
            get
            {
                return _loginCommand ??
                    (_loginCommand = new RelayCommand(param => Login(),
                                                      param => CanLogin()));
            }
        }

        #endregion

        /// <summary>
        /// Constructeur par défaut.
        /// </summary>
        public AuthentificationViewModel()
        {
            _serviceCaller = CommonData.GetServiceCaller();
            ErrorPanelVisibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Méthode indiquant si une opération de Login peut être effectuée.
        /// </summary>
        /// <returns></returns>
        private bool CanLogin()
        {
            return !string.IsNullOrEmpty(UserName) &&
                   !string.IsNullOrEmpty(Password);
        }

        /// <summary>
        /// Méthode de login.
        /// </summary>
        private void Login()
        {
            bool valid = _serviceCaller.CheckLoginAndPassword(UserName, Password);
            if (valid)
            {
                CommonData.LOGGED_USER = _serviceCaller.GetUserByLogin(UserName);
                DialogManager.ShowMainMenu();
                DialogManager.ChangeBackGround();
            }

            SetErrorPanelVisible(valid);
        }

        /// <summary>
        /// Méthode d'affichage ou de masquage du panel.
        /// </summary>
        /// <param name="isVisible"></param>
        private void SetErrorPanelVisible(bool isVisible)
        {
            Visibility visibility = isVisible ? Visibility.Collapsed : Visibility.Visible;
            if (ErrorPanelVisibility != visibility)
                ErrorPanelVisibility = visibility;
        }
    }
}
